/**
 * Project Name:member-with-soa
 * File Name:JacksonUtil.java
 * Package Name:com.lppz.util
 * Date:2015-7-29上午8:08:13
 * Copyright (c) 2015, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.zhzy.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimeZone;


/**
 * ClassName:JacksonUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015-7-29 上午8:08:13 <br/>
 * @author   ShionWong
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
public class JacksonUtil {
	private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
	private static final ObjectMapper mapper = new ObjectMapper();
	
	static {
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.setTimeZone(TimeZone.getDefault());
	}
	
	public static final <T> T parseObject(String input, TypeReference<T> type) {
		try {
			return mapper.readValue(input, type);
		} catch (Exception e) {
			logger.error("jackson parse to bean error,["+input+"]", e);
			return null;
		}
	}
	
	public static final <T> T parseObject(String input, Class<T> clazz) {
		try {
			return mapper.readValue(input, clazz);
		} catch (Exception e) {
			logger.error("jackson parse to bean error,["+input+"]", e);
			return null;
		}
	}
	
	public static String toJSONString(Object obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			logger.error("parse to json String error", e);
			return null;
		}
	}
	
	public static String JsonToString(final Object data) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			String s = mapper.writeValueAsString(data);
			logger.info("{}"+s);
			return s;
		} catch (Exception e) {
			return null;
		}
	}
}

